<?
/*
** Searches the passed variable for Link-Like text then inserts HTML Links around them then returns the edited string.
**
** @return string
**/
function helper_href($var=""){
	$var=preg_replace("/\b(www\S+)/i","http://\\1",$var);
	$var=preg_replace("/\b(([0-9]{1,3}\.){3}[0-9]{1,3}\b)/i","http://\\1",$var);
	$var=preg_replace("/\b((http|ftp)\S+)\b/i","<a href=\"\\1\" target=\"_blank\">\\1</a>",$var);
	#$var=preg_replace("/\b((\S+@)\S+)\b/i","<a href=\"mailto:\\1\" target=\"_blank\">\\1</a>",$var);
	$var=preg_replace("/\b(http:\/\/http:\/\/)/i","http://",$var);
	return($var);
}

/*
** Turns a string that is a plural into a singular string (i.e. businesses=>business, users=>user).
**
** @return string
**/
function helper_singular($var=""){
	if(strtolower(substr($var,-1))=="s"){
		if(strtolower(substr($var,-2))!="ss"){
			if(strtolower(substr($var,-2))=="es"&&strtolower(substr($var,-3))=="ses"){
				$var=substr($var,0,-2);
			}else{
				$var=substr($var,0,-1);
			}
		}
	}
	return($var);
}

/*
** Turns a string that is singular into a plural string (i.e. business=>businesses, user=>users, users=>users').
**
** @return string
**/
function helper_plural($var=""){
	if(strtolower(substr($var,-1))=="s"){
		$var.="'";
	}else{
		$var.="'s";
	}
	return($var);
}

/*
** Returns .
**
** @return string
**/
function helper_left($var="",$chars=2000,$breakUpNice="sentence",$end="..."){
	$result=$var;
	$breakBy="word";
	if(strlen($result)>$chars){
		if(is_bool($breakUpNice)){
			#gravy_debug("isbool",$breakUpNice);
			if(!$breakUpNice){
				#gravy_debug("char",$breakUpNice);
				$breakBy="char";
			}
		}elseif(is_string($breakUpNice)){
			#gravy_debug("isstring",$breakUpNice);
			if($breakUpNice=="char"){
				#gravy_debug("char2",$breakUpNice);
				$breakBy="char";
			}elseif($breakUpNice=="sentence"){
				#gravy_debug("sentence",$breakUpNice);
				$breakBy="sentence";
			}
		}
		$result=substr($result,0,$chars);
		if($breakBy=="char"){
			$result.=$end;
		}else{
			$break=($breakBy=="word")?" ":"\\.";
			$tmp=split($break,$result);
			if(count($tmp)>1){
				#gravy_debug("tmp>1",$tmp);
				$tmp=array_slice($tmp,0,-1);
				$result=join(str_replace("\\","",$break),$tmp);
				if($breakBy=="sentence"&&substr($result,-1)!="."){
					$result.=".";
				}elseif($breakBy=="word"){
					$result.=$end;
				}
			}else{
				#gravy_debug("tmp<=1",$tmp);
				$result=$tmp[0];
				if($breakBy=="sentence"&&substr_count($result,".")>0&&substr($result,-1)!="."){
					$result.=".";
				}else{
					$tmp=split(" ",$result);
					$tmp=join(" ",array_slice($tmp,0,-1));
					$result=$tmp.$end;
				}
			}
		}
	}
	return($result);
}

function gravy_debug($title="",$var=NULL){
	print(chr(13)."<!-- START FLAG //-->".chr(13));
	print("<h1>$title</h1><pre>".chr(13));
	var_dump($var);
	print("</pre>".chr(13));
	print("<!-- END FLAG //-->".chr(13));
}

function helper_outputJsOpenTag(){
?>
<script type="text/javascript">
	<!--
<?
}

function helper_outputJsCloseTag(){
?>
	//-->
</script>
<?
}

function helper_space_null($var=""){
	$result=$var;
	if(is_null($result)||strlen($result)<1){
		$result="&nbsp;&nbsp;";
	}
	return($result);
}

function helper_js_content($text=""){
	return(formatter::jsText($text));
}

function helper_xml_encode($str){
	return(str_replace(array('&','"',"'",'<', '>'), array ( '&amp;' , '&quot;', '&apos;' , '&lt;' ,'&gt;'),$str));
}

function helper_urlencodearray($array,$encrypt=true){
	$result="";
	$model=new model();
	if(is_array($array)){
		foreach($array as $key=>$value){
			if((substr($key,-3)=="_id"||$key=="id")&&$encrypt){
				$value=$model->num_encrypt($value,$array["dateAdded"]);
			}
			$result.=urlencode($key)."=".urlencode($value)."&";
		}
		if(strlen($result)>0){
			$result=substr($result,0,-1);
		}
	}
	return($result);
}
?>